local ads__jiejia = fk.CreateSkill {
  name = "ads__jiejia",
}

local jiejia_spec = {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(ads__jiejia.name) and
        Exppattern:Parse(data.pattern):matchExp("slash") and
        (data.extraData == nil or data.extraData.ads__jiejia_ask == nil) and
        not table.every(player.room.alive_players, function(p)
          return p == player or p.kingdom == "wei"
        end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p:isAlive() and p.kingdom ~= "wei" then
        local params = { ---@type AskToUseCardParams
          skill_name = "ads__jiejia",
          pattern = "slash",
          prompt = "#ads__jiejia-ask:" .. player.id,
          cancelable = true,
          extra_data = { ads__jiejia_ask = true }
        }
        local respond = room:askToResponse(p, params)
        if respond then
          respond.skipDrop = true
          room:responseCard(respond)

          local new_card = Fk:cloneCard('slash')
          new_card.skillName = "ads__jiejia"
          new_card:addSubcards(room:getSubcardsByRule(respond.card, { Card.Processing }))
          local result = {
            from = player,
            card = new_card,
          }
          if event == fk.AskForCardUse then
            result.tos = {}
          end
          data.result = result
          return true
        end
      end
    end
  end,
}

ads__jiejia:addEffect(fk.AskForCardUse, jiejia_spec)
ads__jiejia:addEffect(fk.AskForCardResponse, jiejia_spec)

ads__jiejia:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "slash",
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 then return end
    local c = Fk:cloneCard("slash")
    c.skillName = ads__jiejia.name
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    if use.tos then
      room:doIndicate(player.id, table.map(use.tos, Util.IdMapper))
    end

    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p:isAlive() and p.kingdom ~= "wei" then
        local respond = room:askToResponse(p, {
          skill_name = ads__jiejia.name,
          pattern = "slash",
          prompt = "#ads__jiejia-ask:"..player.id,
          cancelable = true,
        })
        if respond then
          respond.skipDrop = true
          room:responseCard(respond)

          use.card = respond.card
          return
        end
      end
    end

    room:setPlayerMark(player, "ads__jiejia_failed-phase", 1)
    return ads__jiejia.name
  end,
  enabled_at_play = function(self, player)
    return player:getMark("ads__jiejia_failed-phase") == 0 and
      table.find(Fk:currentRoom().alive_players, function(p)
        return p.kingdom ~= "wei" and p ~= player
      end)
  end,
  enabled_at_response = function(self, player)
    return table.find(Fk:currentRoom().alive_players, function(p)
      return p.kingdom ~= "wei" and p ~= player
    end)
  end,
})


Fk:loadTranslationTable{
  ["$ads__jiejia1"] = "来人！护驾！",
  ["$ads__jiejia2"] = "魏将何在？",
  ["ads__jiejia"] = "劫驾",
  [":ads__jiejia"] = "当你需要使用或打出【杀】时，你可以令其他非魏势力角色选择是否打出一张【杀】（视为由你使用或打出）。",
  ["#ads__jiejia-ask"] = "劫驾：你可打出一张杀，视为 %src 使用或打出",
}


ads__jiejia:addAI({
  think_skill_invoke = Util.TrueFunc,
})

return ads__jiejia
